<?php
class SelectList
{
    protected $conn;
 
	public function __construct()

	{
		$this->DbConnect();
	}
	
	protected function DbConnect()
	{
		include "db_config.php";
		$this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
		mysql_select_db($db,$this->conn) OR die("can not select the database $db");
		return TRUE;
	}
	
	public function ShowCategory()
	{
		$sql = "SELECT * FROM categories WHERE parent_category_id=0";
		$res = mysql_query($sql,$this->conn);
		$category = '<option value="">Department</option>';
		while($row = mysql_fetch_array($res))
		{
			$category .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
		}
		return $category;
	}
	
	
	public function ShowType()
	{
		$sql = "SELECT * FROM categories WHERE parent_category_id=$_POST[id]";
		$res = mysql_query($sql,$this->conn) or die(mysql_error());
		$type = '<option value="">choose...</option>';
		while($row = mysql_fetch_array($res))
		{
			$type .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
		}
		return $type;
	}
	
	public function DisplaySearch()
	{
        echo '<form id="select_form" method="get" action="search.php">
            <select id="category" name="category">'.$this->ShowCategory();
        echo '</select>
        <select id="type" name="type">
             <option value="0">choose...</option>
        </select>
		<input type="text" name="manual_search" size=50>
        <input type="submit" />
        </form>
        <div id="result"></div>';
	}
	
	public function IncludeSearchFunc()
	{
		echo '<script type="text/javascript" src="jquery-1.9.1.js"></script>
				<script type="text/javascript">
					$(document).ready(function(){
						$("select#type").attr("disabled","disabled");
						$("select#category").change(function(){
						$("select#type").attr("disabled","disabled");
						$("select#type").html("<option>wait...</option>");
						var id = $("select#category option:selected").attr("value");
						$.post("select_type.php", {id:id}, function(data){
							$("select#type").removeAttr("disabled");
							$("select#type").html(data);
						});
					});
				});
				</script>';
	}
}

$opt = new SelectList();
?>